<template>
  <div class="appointment">
    <div class="a-gantt">
      <el-row style="padding: 12px 10px; background-color: #fff">
        <el-col :span="12" align="left" style="font-weight: 700">
          Echarts版超低仿甘特图
        </el-col>
        <el-col :span="12" align="right">
          <el-date-picker
            v-model="baseDate"
            type="date"
            @change="handleSelect"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-col>
      </el-row>
    </div>
    <div class="f-gantt">
      <Gantt
        :baseDate="baseDate"
        ref="gantt"
        :ganttData="ganttData"
        @getInfoCallback="getGanttInfo"
        :roomData="roomData"
      ></Gantt>
    </div>
    <!-- 新增编辑框 -->
    <el-dialog :title="formTitle" :visible.sync="dialogVisible" width="30%">
      <el-form :model="form" :label-width="formLabelWidth">
        <el-form-item label="会议室">
          <el-input
            disabled
            v-model="roomData[form.index]"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm"
          >
          </el-date-picker>
          -
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="form.status" style="width: 100%">
            <el-option label="进行中" value="0"></el-option>
            <el-option label="已完成" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Gantt from './Gantt'
export default {
  name: 'Home',
  components: {
    Gantt
  },
  data() {
    return {
      baseDate: `${new Date().getFullYear()}-${
        new Date().getMonth() + 1
      }-${new Date().getDate()}`,
      roomData: [],
      ganttData: [],
      dialogVisible: false,
      formLabelWidth: '120px',
      formTitle: '',
      form: {
        id: '',
        index: '',
        content: '',
        endTime: '',
        status: '',
        startTime: ''
      }
    }
  },
  created() {
    // debugger
    this.ganttData = [
      {
        value: [
          {
            index: 1,
            roomName: '会议室二',
            RoomId: '123',
            id: '333',
            startTime: `${this.baseDate} 8:28`,
            endTime: `${this.baseDate} 9:28`,
            status: '1',
            content: '睡觉'
          }
        ]
      },
      {
        value: [
          {
            index: 0,
            roomName: '会议室一',
            RoomId: '2234',
            id: '444',
            startTime: `${this.baseDate} 10:28`,
            endTime: `${this.baseDate} 12:28`,
            status: '0',
            content: '吃饭'
          }
        ]
      }
    ]
    this.roomData = ['会议室一', '会议室二', '会议室三', '会议室四']
  },
  mounted() {},
  methods: {
    handleSelect() {
      this.$refs.gantt.myEcharts()
    },
    getGanttInfo(data) {
      this.dialogVisible = true
      // 根据data的长度判断是新增还是编辑
      // 新增
      if (data.length === 2) {
        this.formTitle = '新增'
        this.form = this.$options.data().form
        this.$set(this.form, 'index', data[1])
      } else {
        this.formTitle = '修改'
        this.form = data[0]
      }
    },
    saveData() {
      if (this.formTitle === '修改') {
        this.ganttData = this.ganttData.filter((item) => {
          return item.value[0].id !== this.form.id
        })
      } else {
        this.$set(this.form, 'status', '0')
      }
      const obj = Object.assign({}, this.form)
      this.ganttData.push({ value: [obj] })
      this.$refs.gantt.myEcharts()
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
.a-gantt {
  position: absolute;
  top: 0;
  height: 60px;
  width: 100%;
  box-sizing: border-box;
  text-align: center;
}

.appointment {
  position: relative;
  height: 100%;
  /* margin: 0 24px; */
  overflow-y: hidden;
  /* min-width: 1131px; */
  border: 1px solid #ddd;
  color: #0f1419;
  box-sizing: border-box;
}
.f-gantt {
  position: absolute;
  bottom: 0;
  top: 80px;
  width: 100%;
  height: 600px;
  box-sizing: border-box;
}
</style>
